global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A3_sample.log, replace name(tabl)

capture noi {

  * Get summary statistics on patent applications, families and firms


  texdoc init ${tab_dir}/appendix/Table_A3_sample.tex, replace
  texdoc stlog, nolog

  use ${final_dir}/sample_descriptives.dta, clear
  keep if auto95 == 1

  * Number of applications, families and biadic families in Patstat and in Patstat Orbis
  local pattypes applications families bia_families
  foreach pattype of local pattypes {  
      preserve
      if ("`pattype'" == "applications") keep if uniq_appln == 1
      if ("`pattype'" == "families" | "`pattype'" == "bia_families") keep if uniq_fam == 1
      if ("`pattype'" == "bia_families") qui keep if bia == 1

      qui count
      di "Number of auto95 `pattype' in Patstat: `r(N)'"
      
      qui count if in_period == 1
      di "Number of auto95 `pattype' in Patstat in 1997-2011: `r(N)'"
        sleep 700
      texdoc local `pattype'_patstat "\num{`r(N)'}"

      qui count if in_orbis == 1
      di "Number of auto95 `pattype' matched with Orbis: `r(N)'"
      
      qui count if in_orbis == 1 & in_period == 1 
      di "Number of auto95 `pattype' matched with Orbis in 1997-2011: `r(N)'"
        sleep 700
      texdoc local `pattype'_orbis "\num{`r(N)'}"
      restore
  } 

  * Number of applications, families and biadic families associated with regression firms
  qui count if appln_in_sample == 1 & uniq_appln == 1
  di "Number of auto95 applications in regression sample: `r(N)'"
    sleep 700
  texdoc local applications_sample "\num{`r(N)'}"

  qui count if bia == 1 & fam_in_sample == 1 & uniq_appln == 1
  di "Number of auto95 applications associated with the biadic auto95 patent families in our firm-level regression dataset: `r(N)'"

  qui count if fam_in_sample == 1 & uniq_fam == 1
  di "Number of auto95 families in regression sample: `r(N)'"
    sleep 700
  texdoc local families_sample "\num{`r(N)'}"

  qui count if fam_in_sample == 1 & uniq_fam == 1 & bia == 1
  di "Number of auto95 biadic families in regression sample: `r(N)'"
    sleep 700
  texdoc local bia_families_sample "\num{`r(N)'}"

  * Number of firms
  use ${final_dir}/sample_descriptives, clear 
  count if uniq_firm == 1 & in_orbis == 1
  di "Number of firms matched with Orbis: `r(N)'"

  keep if in_period == 1 & in_orbis == 1 
  duplicates drop BvD, force
  count
  di "Number of firms that patent at least once in 97-2011: `r(N)'"

  keep if f_does_auto95_bia == 1
  count
  di "Number of firms that do auto95_bia in period: `r(N)'"
    sleep 700
  texdoc local firms_orbis "\num{`r(N)'}"

  keep if f_in_sample == 1 
  count
  di "Number of firms in regression sample: `r(N)'"
    sleep 700
  texdoc local firms_sample "\num{`r(N)'}"


  texdoc local notes "This table reports the number of auto95 patent applications, families, biadic families, and firms (that do at least one auto95 biadic innovation) for the time period 1997-2011 for three different samples based on PATSTAT: the whole sample, the sample of firms observed in ORBIS and the sample of firms included in our baseline regression."
  texdoc stlog close
  include ${code_dir}/config/texdoctools.do
  tex `texpreamble'

  /*tex
  \begin{tabular}{@{}l*{4}S[table-format=6]@{}}
  \toprule
    & \mc{Applications} & \mc{Families} & \mc{Biadic Families} & \mc{Firms} \\
  \midrule
  Patstat 1997-2011      & `applications_patstat' & `families_patstat'& `bia_families_patstat'& {--} \\
  Matched with Orbis     & `applications_orbis'   & `families_orbis'  & `bia_families_orbis'  & `firms_orbis' \\
  Firms in sample        & `applications_sample'  & `families_sample' & `bia_families_sample' & `firms_sample' \\
  \bottomrule
  \end{tabular}

  tex*/
  tex `texpostamble'
  texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}